// pages/location/location.js

const db = wx.cloud.database();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    longitude: "",
    latitude: "",
    polyline: [],
    recordId: 0,
    scale: 14.5,
    timer: undefined,
    hasEnd: false,
    isDisabled: false,
    markers: [
      {
        iconPath: "../images/car.png",
        latitude: "",
        longitude: "",
        width: 16,
        height: 20,
        rotate: 0,
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("页面参数是", options);
    this.setData({
      recordId: Number(options.recordId),
    });
  },
  onShow: function () {
    this.fetchData();
  },
  onHide: function () {
    if (this.data.timer != undefined) {
      clearInterval(this.data.timer);
      this.data.timer = undefined;
    }
  },
  onUnload: function () {
    if (this.data.timer != undefined) {
      clearInterval(this.data.timer);
      this.data.timer = undefined;
    }
  },
  /**
   * 获取数据
   */
  fetchData() {
    if (this.data.recordId == "" || this.data.hasEnd) return;
    let that = this;
    this.requestDb();
    if (that.data.timer != undefined) {
      clearInterval(that.data.timer);
      that.data.timer = undefined;
    }
    that.data.timer = setInterval((r) => {
      that.requestDb();
    }, 1000);
  },

  /**
   * 请求远程db
   */
  requestDb() {
    var that = this;
    var pageCount = 20;
    var points = [];
    if (this.data.polyline.length != 0) {
      points = this.data.polyline[0].points;
    }
    console.log("数组大小:", points.length);
    db.collection("track")
      .where({
        recordId: this.data.recordId,
      })
      .skip(points.length)
      .limit(pageCount)
      .get()
      .then((res) => {
        console.log("请求结果", res);
        let list = res.data || [];
        if (list.length == 0) return;
        points = points.concat(list);
        if (points.length > 0) {
          var lastObj = points[points.length - 1];
          if (lastObj.longitude == -1 || lastObj.latitude == -1) {
            if (this.data.timer != undefined) {
              clearInterval(this.data.timer);
              this.data.timer = undefined;
            }
            that.setData({
              hasEnd: true,
            });
            points.pop();
            wx.showModal({
              title: "本次行程已结束",
              showCancel: false,
              success(result) {},
            });
          } else {
            that.data.longitude = lastObj["longitude"];
            that.data.latitude = lastObj["latitude"];
            that.data.markers[0]["longitude"] = lastObj["longitude"];
            that.data.markers[0]["latitude"] = lastObj["latitude"];
            var rotate = lastObj["direction"] || 0;
            if (rotate != 0) {
              this.data.markers[0]["rotate"] = rotate;
            }
          }

          that.setData({
            longitude: that.data.longitude,
            latitude: that.data.latitude,
            markers: that.data.markers,
            polyline: [
              {
                points: points,
                color: "#4B0082",
                width: 8,
                arrowLine: true,
                dottedLine: false,
              },
            ],
          });
        }
      });
  },
});
